function showRequest(formData, jqForm, options)
{ 
	var queryString = $.param(formData); 
	return true; 
};
 
function showResponse(responseText, statusText)
{ 
	if( $('form').attr('action').indexOf('insert-get-id') != -1 && isNumeric(responseText) )
	{
		var form = $('form').attr('action').replace("insert-get-id","form");
		show_aviso("Salvando dados...");
		setTimeout(
				function()
				{
					window.location.href = form + '/id/'+ responseText;
					return false;
				}
				, 2000
			)
	}
	else if( $('form').attr('action').indexOf('update-get-id') != -1 && isNumeric(responseText) )
	{
		show_aviso("Dados alterados com sucesso");
		hide_aviso();
	}
	else
	{
		$("#avisos").show();
		hide_aviso();
	}
};

if( $('form input#id').val() == undefined)
{
	var options = { 
		target       :'#avisos', 
		beforeSubmit :showRequest, 
		success      :showResponse,
		resetForm    :true
	};
}
else
{
	var options = { 
		target       :'#avisos', 
		beforeSubmit :showRequest, 
		success      :showResponse
	};
};

$('form').ajaxForm(options);

$('.voltar').click(function()
{
	var form = $('form').attr('action');
	form = form.replace("insert-get-id","grid");
	form = form.replace("update-get-id","grid");
	form = form.replace("insert","grid");
	form = form.replace("update","grid");		
	
	window.location.href = form;
	return false;
});
